package com.qcby.springbootdemotest.controller;

import com.qcby.springbootdemotest.Util.AddressMap;
import com.qcby.springbootdemotest.Util.StringToNumber;
import com.qcby.springbootdemotest.dao.ZhiLianDao;
import com.qcby.springbootdemotest.entity.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/zhilian")
public class ZhiLianController {

    @Autowired
    private ZhiLianDao zhiLianDao; //这里报错我们不管

    /**
     * 跳转到前端页面
     * @return
     */
    @RequestMapping("toWorm")
    public String worm(){
        return "/Worm";
    }


    @RequestMapping("/insert")
    @ResponseBody   //加上它返回的是字符串数据，如果不加返回页面
    public void wormInsert(ZhiLian zhiLian) throws IOException, InterruptedException {
        //根据前端传来的值进行url的拼接
        String address = zhiLian.getAddress();
        String addressCode = AddressMap.addressMap.get(zhiLian.getAddress());
        String sector = zhiLian.getSector();
       /* String url2 = "https://sou.zhaopin.com/?jl="+address+"&kw="+sector+"&p="+1;
        System.out.println("url2=" + url2);*/
        String url = "https://sou.zhaopin.com/?jl="+addressCode+"&kw="+sector+"&p="+1;
        System.out.println("url=" + url);
        Document scriptHtml = Jsoup.connect(url)
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                .header("Accept-Encoding", "gzip, deflate, br, zstd")
                .header("Accept-Language", "zh-CN,zh;q=0.9")//,en-US;q=0.5,en;q=0.3
                .header("Cache-Control","max-age=0")
                .header("Cookie", "x-zp-client-id=800ca9ea-8d55-447f-8ead-005541ae2071; sts_deviceid=1907b606fc24bc-066552e7f60a7f-26001f51-1327104-1907b606fc3cff; selectCity_search=570; zp_passport_deepknow_sessionId=a09b3c41s24e744d1d819831f5c74e9a954d; sts_sg=1; sts_chnlsid=Unknown; zp_src_url=https%3A%2F%2Flanding.zhaopin.com%2F; ZL_REPORT_GLOBAL={%22/resume/new%22:{%22actionid%22:%2265ae309d-bd19-47f2-a4da-6616d1a9eebd%22%2C%22funczone%22:%22addrsm_ok_rcm%22}}; _uab_collina=172027212652809767606209; Hm_lvt_7fa4effa4233f03d11c7e2c710749600=1720272128; HMACCOUNT=BDC943248E54B93C; LastCity=%E4%BF%9D%E5%AE%9A; LastCity%5Fid=570; sensorsdata2015jssdkchannel=%7B%22prop%22%3A%7B%22_sa_channel_landing_url%22%3A%22https%3A%2F%2Flanding.zhaopin.com%2Fregister%3Fidentity%3Dc%26channel_name%3Dbaidu_sem_track%26callback_id%3DIQJ0tYqm%26_data_version%3D0.6.1%26channel_utm_content%3Dpp%26project%3Dzlclient%26channel_utm_medium%3Dcpt%26tid%3DxTB%26channel_link_type%3Dweb%26channel_utm_source%3Dbaidupcpz_x%26hash_key%3DuaKIYNjV6PvgIWUUAJ9n%26sat_cf%3D2%26channel_utm_campaign%3Dzbt%26channel_utm_term%3D01%26_channel_track_key%3DM9kzlBZB%26link_version%3D1%26channel_keyword_id%3D%7Bkeywordid%7D%26channel_ad_id%3D%7Bcreative%7D%26channel_account_id%3D%7Buserid%7D%26channel_keyword%3D%7Bkw_enc_utf8%7D%26channel_adgroup_id%3D%7Bunitid%7D%26channel_campaign_id%3D%7Bplanid%7D%22%7D%7D; locationInfo_search={%22code%22:%22570%22%2C%22name%22:%22%E4%BF%9D%E5%AE%9A%22%2C%22message%22:%22%E5%8C%B9%E9%85%8D%E5%88%B0%E5%B8%82%E7%BA%A7%E7%BC%96%E7%A0%81%22}; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221204446488%22%2C%22first_id%22%3A%221907b498970618-01eab826c4cb1d4-26001f51-1327104-1907b498971daf%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E4%BB%98%E8%B4%B9%E5%B9%BF%E5%91%8A%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%99%BA%E8%81%94%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Fother.php%22%2C%22%24latest_utm_source%22%3A%22%E9%A6%96%E9%A1%B5%22%2C%22%24latest_utm_medium%22%3A%22button%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkwN2I0OTg5NzA2MTgtMDFlYWI4MjZjNGNiMWQ0LTI2MDAxZjUxLTEzMjcxMDQtMTkwN2I0OTg5NzFkYWYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIxMjA0NDQ2NDg4In0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%221204446488%22%7D%2C%22%24device_id%22%3A%221907b498970618-01eab826c4cb1d4-26001f51-1327104-1907b498971daf%22%7D; at=2329a53a9e66400bb04cd864e57c3bd0; rt=e78f5532dca94fa1b6bbb36780703a22; acw_tc=2760828b17206831982165451ecfb308eae43644529e49c61967ac0a184210; Hm_lpvt_7fa4effa4233f03d11c7e2c710749600=1720683200")
                .header("Priority","u=0, i")
                .header("Referer","https://www.zhaopin.com/")
                .header("Sec-Ch-Ua","\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Microsoft Edge\";v=\"126\"")
                .header("Sec-Ch-Ua-Mobile","?0")
                .header("Sec-Ch-Ua-Platform","\"Windows\"")
                .header("Sec-Fetch-Dest","document")
                .header("Sec-Fetch-Mode","navigate")
                .header("Sec-Fetch-Site","same-origin")
                .header("Sec-Fetch-User","?1")
                .header("Upgrade-Insecure-Requests","1")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0")
                .timeout(50000)
                .get();
        Elements content = scriptHtml.getElementsByClass("joblist-box__item");
        for(Element element:content){
            String company = element.getElementsByClass("companyinfo__name").text();
            String salary = element.getElementsByClass("jobinfo__salary").text();
            String experience = element.getElementsByClass("jobinfo_other-info-item").text();
            String education = element.getElementsByClass("jobinfo_other-info-item").text();
            String industary = element.getElementsByClass("joblist-box__item-tag").text();

            //判断金额当中是否包含天
            if(salary.contains("天")||salary.indexOf("-")==-1){
                break; //包含天这个数据不能使用  没有“-”的数据不用
            }else {
                String s1 = StringToNumber.getValue(salary); // 获取最小的金额
                Double money = StringToNumber.StrToNum(s1) * 12;
                zhiLian.setSalary(String.valueOf(money));
            }
            zhiLian.setCompany(company);
           // System.out.println(company+" "+salary+" "+experience+" "+education);
            zhiLian.setExperience(experience);
            zhiLian.setEducation(education);
            zhiLian.setEducation(industary);

            zhiLianDao.insert(zhiLian);
        }

        }


    @RequestMapping("/findBySalaryAndSector")
    @ResponseBody
    public List<SalaryAndSector> findBySalaryAndSector(){
        return zhiLianDao.findBySalaryAndSector();
    }

    @RequestMapping("/findCountBySectorAndAddress")
    @ResponseBody
    public List<SectorAndAddress> findCountBySectorAndAddress(String address){
        return zhiLianDao.findCountBySectorAndAddress(address);
    }

    @RequestMapping("/findSalaryByAddressAndSector")
    @ResponseBody
    public List<SalaryByAddressAndSector> findSalaryByAddressAndSector(String address){
        return zhiLianDao.findSalaryByAddressAndSector(address);
    }

    @RequestMapping("/findSalaryBySectorAndAddress")
    @ResponseBody
    public List<SalaryBySectorAndAddress> findSalaryBySectorAndAddress(String sector){
        return zhiLianDao.findSalaryBySectorAndAddress(sector);
    }

    @RequestMapping("/findCountByAddressAndEducation")
    @ResponseBody
    public List<AddressAndEducation> findCountByAddressAndEducation(String address){
        return zhiLianDao.findCountByAddressAndEducation(address);
    }

    @RequestMapping("/findCountBySectorAndExperience")
    @ResponseBody
    public List<SectorAndExperience> findCountBySectorAndExperience(String sector){
        return zhiLianDao.findCountBySectorAndExperience(sector);
    }

    @RequestMapping("/findAddressAndCount")
    @ResponseBody
    public List<AddressAndCount> findAddressAndCount(){
        return zhiLianDao.findAddressAndCount();
    }
}


